package com.atguigu.controller;

import com.atguigu.en.HouseImageTypeEnum;
import com.atguigu.entity.*;
import com.atguigu.result.Result;
import com.atguigu.service.*;
import com.atguigu.vo.HouseQueryVo;
import com.atguigu.vo.HouseVo;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * JavaBean的分类：
 * POJO：简单的java对象，通常用于与数据库进行交互(携带数据给数据库、接收数据库查询出来的数据)
 * VO：视图对象，通过用于与视图进行交互(接收客户端携带的请求参数、封装给客户端的响应数据)
 * TO：传输对象，通常用于分布式架构中，远程调用的时候传输数据
 * BO：业务对象，它里边除了可以封装数据，还有一些业务操作方法
 */


@RestController
@RequestMapping("/house")
public class HouseController {

    @DubboReference
    private HouseService houseService;

    @DubboReference
    private CommunityService communityService;

    @DubboReference
    private HouseBrokerService houseBrokerService;

    @DubboReference
    private HouseImageService houseImageService;

    @DubboReference
    private UserFollowService userFollowService;

    /**
     * 请求方式：post请求
     * @param pageNum
     * @param pageSize
     * @param houseQueryVo
     * @return
     */
    @PostMapping("/list/{pageNum}/{pageSize}")
    public Result list(@PathVariable("pageNum") Integer pageNum,
                       @PathVariable("pageSize") Integer pageSize,
                       @RequestBody HouseQueryVo houseQueryVo) {
        //调用业务层方法查询前台房源分页数据
        PageInfo<HouseVo> pageInfo = houseService.findListPage(pageNum, pageSize, houseQueryVo);
        return Result.ok(pageInfo);
    }

    @GetMapping("/lastedList")
    public Result lastedList() {
        //调用业务层方法查询最新上架的房源列表
        List<House> lastedList = houseService.findLatedList();
        return Result.ok(lastedList);
    }

    //查看房源详情
    @GetMapping("/info/{houseId}")
    public Result info(@PathVariable("houseId") Long houseId, HttpSession session) {
        //调用业务层的方法查询房源详情
        //1.查询房源信息
        House house = houseService.getById(houseId);
        //2.查询小区信息
        Community community = communityService.getById(house.getCommunityId());
        //3.经纪人列表
        List<HouseBroker> houseBrokerList = houseBrokerService.findHouseBrokerListByHouseId(houseId);
        //4.房源图片列表
        List<HouseImage> houseImage1List = houseImageService.findHouseImageListByHouseId(houseId, HouseImageTypeEnum.HOUSE_SOURCE.getCode());
        //5.当前用户是否关注了该房源
        boolean isFollow = false;
        UserInfo userInfo = (UserInfo) session.getAttribute("USER");
        if (userInfo != null) {//已登录
            //根据userId和houseId到user_follow表查询数据
            if (userFollowService.findByUserIdAndHouseId(userInfo.getId(), houseId) != null) {
                isFollow = true;
            }
        }
        //将上述5个数据封装到Map对象
        Map resultMap = new HashMap();
        resultMap.put("community", community);
        resultMap.put("house", house);
        resultMap.put("houseBrokerList", houseBrokerList);
        resultMap.put("houseImage1List", houseImage1List);
        //resultMap.put("houseUserList", isFollow);
        resultMap.put("isFollow", isFollow);
        return Result.ok(resultMap);
    }


}
